//# sourceURL=http://uMapApp/Module/Camera/CameraBridgeModule
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../../UI/Panel/PanelBlock/TreeBlock"
    , "../../UI/Tree/Tree"
    , "../../UI/Panel/PanelBlock/PropertiesBlock"
    , "./CameraBridge"
], function (
    mhjs
    , uMap
    , uEarth
    , Module
    , FloatPanel
    , TreeBlock
    , Tree
    , PropertiesBlock
    , CameraBridge
) {

    function CameraBridgeModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "桥梁视频";
        _this.icon = _this.app.icons.camera;
        _this.panels = [];
        _this.items = [];

        _this.listPanel = new FloatPanel({"class": "rightPanel", "title": "桥梁监控视频列表"});

        if (_this.standalone) {
            _this.listPanel.showMinMax = true;
        } else {
            _this.listPanel.showClose = true;
        }


        _this.listPanel.listBlock = new TreeBlock({"class": "fullBlock"});
        _this.listPanel.listBlock.title = _this.title;
        _this.listPanel.append(_this.listPanel.listBlock);

        _this.listPanel.on("close", function () {
            _this.closePanel();
        });

        _this.panels.push(_this.listPanel);

        // _this.detailPanel = new FloatPanel({"class": "leftPanel", "showClose": true});
        // _this.detailPanel.propertiesBlock = new PropertiesBlock({"class": "fullBlock"});
        // _this.detailPanel.append(_this.detailPanel.propertiesBlock);
        //
        // _this.detailPanel.on("open", function () {
        //     _this.trigger("openDetail");
        // });
        //
        // _this.detailPanel.on("close", function () {
        //     _this.trigger("closeDetail");
        //     _this.unActiveAllItem();
        // });
        //
        // _this.panels.push(_this.detailPanel);

        _this.app.ui.layout.right.append(_this.listPanel);
        // _this.app.ui.layout.left.append(_this.detailPanel);
    }

    var _class = CameraBridgeModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"             : {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"       : {
            value: function (list) {

                var _this = this;

                _this.dealTree(list, _this.listPanel.listBlock.tree);

                // $.each(list, function (i, catalog) {
                //
                //     if (catalog.dataObj) {
                //
                //         var siteTree = new Tree({
                //             "title": catalog.name,
                //             "list" : []
                //         });
                //
                //         $.each(catalog.children, function (j, item) {
                //
                //             if (_this.objMap) {
                //                 item = _this.objMap.format(item);
                //             }
                //
                //             item.app = _this.app;
                //             item.icon = _this.icon;
                //
                //             if (_this.extendInfos && _this.extendInfos[item["编号"]]) {
                //
                //                 if (_this.extendInfos[item["编号"]].position) {
                //                     item.position = _this.extendInfos[item["编号"]].position;
                //                 }
                //
                //                 if (_this.extendInfos[item["编号"]].model) {
                //                     item.model = _this.extendInfos[item["编号"]].model;
                //                 }
                //
                //                 if (_this.extendInfos[item["编号"]].viewport) {
                //                     item.viewport = _this.extendInfos[item["编号"]].viewport;
                //                 }
                //
                //             }
                //
                //             var cameraBridge = new CameraBridge(item);
                //             _this.items.push(cameraBridge);
                //
                //             cameraBridge.listItem = new Tree({
                //                 "title"       : cameraBridge["名称"],
                //                 "subtitle"    : cameraBridge["编号"],
                //                 "icon"        : _this.icon.base64,
                //                 "cameraBridge": cameraBridge
                //             });
                //
                //             cameraBridge.propertiesBlock = new PropertiesBlock();
                //             cameraBridge.propertiesBlock.title = cameraBridge.title;
                //             cameraBridge.propertiesBlock.map = _this.propertiesMap;
                //             cameraBridge.propertiesBlock.properties = cameraBridge;
                //
                //             cameraBridge.on("click", function () {
                //                 _this.activeItem(cameraBridge);
                //                 _this.app.pop.target = cameraBridge;
                //                 _this.app.pop.content = cameraBridge.propertiesBlock;
                //                 _this.app.pop.position = cameraBridge.position;
                //                 _this.app.pop.open();
                //             });
                //
                //             cameraBridge.on("close", function (cameraBridge) {
                //                 if (_this.app.pop.target === cameraBridge) {
                //                     _this.app.pop.close();
                //                 }
                //             });
                //
                //             cameraBridge.listItem.on("click", function () {
                //                 _this.activeItem(cameraBridge);
                //                 cameraBridge.layer && cameraBridge.layer.active();
                //
                //                 // _this.detailPanel.propertiesBlock.title = camera["名称"];
                //                 // _this.detailPanel.propertiesBlock.map = _this.propertiesMap;
                //                 // _this.detailPanel.propertiesBlock.properties = camera;
                //                 // _this.detailPanel.open();
                //             });
                //
                //             siteTree.add(cameraBridge.listItem);
                //
                //         });
                //
                //         _this.listPanel.listBlock.tree.add(siteTree);
                //
                //     }
                //
                //
                // });
            }
        },
        "clear"            : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "open"             : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {
                    _this.isOpened = true;

                    _this.app.modules.road.bridgeModule.openNoMark();

                    $.each(_this.items, function (i, item) {
                        item.open();
                    });

                    if (_this.standalone) {
                        _this.listPanel.open(false);
                    }

                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadPosition"     : {
            value: function (callback) {

                var _this = this;

                if (_this.positionRequest) {
                    _this.positionRequest.send({}, function (json) {
                        callback && callback(json);
                    });
                } else {
                    callback && callback();
                }
            }
        },
        "loadInit"         : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.load({}, function (data, mapData) {

                        _this.loadPosition(function (extendJson) {

                            if (mapData) {
                                _this.objMap = mapData.obj;
                                _this.propertiesMap = mapData.properties;
                            }

                            if (extendJson && extendJson.data) {
                                _this.extendInfos = extendJson.data;
                            }

                            _this.data = data;

                            _this.isLoaded = true;
                            callback && callback();

                        });
                    });

                } else {
                    callback && callback();
                }


            }
        },
        "openPanel"        : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"            : {
            value: function () {
                var _this = this;
                _this.closePanel(false);
                _this.unActiveAllItem();
                $.each(_this.items, function (i, item) {
                    item.close();
                });

                _this.app.modules.road.bridgeModule.close();

                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "activeItem"       : {
            value: function (item) {
                var _this = this;
                if (!item.isActive) {
                    _this.unActiveAllItem();
                    item.active();
                    item.listItem.addClass("active");
                    item.listItem.parent().animate({"scrollTop": item.listItem.dom[0].offsetTop});
                }
            }
        },
        "unActiveAllItem"  : {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        },
        "dealTree"         : {
            value: function (tree, rootDom) {

                var _this = this;

                $.each(tree, function (i, item) {

                    if (item.dataObj && item.dataObj.channelCode) {
                        //子节点

                        if (_this.objMap) {
                            item = _this.objMap.format(item);
                        }

                        item.app = _this.app;
                        item.icon = _this.icon;

                        _this.setCameraPosition(item);

                        // if (_this.extendInfos && _this.extendInfos[item["编号"]]) {
                        //
                        //     if (_this.extendInfos[item["编号"]].position) {
                        //         item.position = _this.extendInfos[item["编号"]].position;
                        //     }
                        //
                        //     if (_this.extendInfos[item["编号"]].model) {
                        //         item.model = _this.extendInfos[item["编号"]].model;
                        //     }
                        //
                        //     if (_this.extendInfos[item["编号"]].viewport) {
                        //         item.viewport = _this.extendInfos[item["编号"]].viewport;
                        //     }
                        //
                        // }

                        var cameraBridge = new CameraBridge(item);
                        _this.items.push(cameraBridge);

                        cameraBridge.listItem = new Tree({
                            "title"       : cameraBridge["名称"],
                            "subtitle"    : cameraBridge["编号"],
                            "icon"        : _this.icon.base64,
                            "cameraBridge": cameraBridge
                        });

                        cameraBridge.propertiesBlock = new PropertiesBlock();
                        cameraBridge.propertiesBlock.title = cameraBridge.title;
                        cameraBridge.propertiesBlock.map = _this.propertiesMap;
                        cameraBridge.propertiesBlock.properties = cameraBridge;

                        cameraBridge.on("click", function () {
                            _this.activeItem(cameraBridge);
                            _this.app.pop.target = cameraBridge;
                            _this.app.pop.content = cameraBridge.propertiesBlock;
                            _this.app.pop.position = cameraBridge.position;
                            _this.app.pop.open();
                        });

                        cameraBridge.on("unActive", function (cameraBridge) {
                            if (_this.app.pop.target === cameraBridge) {
                                _this.app.pop.close(false);
                            }
                        });

                        cameraBridge.listItem.on("click", function () {
                            _this.activeItem(cameraBridge);
                            cameraBridge.layer && cameraBridge.layer.active();

                            // _this.detailPanel.propertiesBlock.title = camera["名称"];
                            // _this.detailPanel.propertiesBlock.map = _this.propertiesMap;
                            // _this.detailPanel.propertiesBlock.properties = camera;
                            // _this.detailPanel.open();
                        });

                        rootDom.add(cameraBridge.listItem);


                    } else if (item.children) {
                        //文件夹

                        //屏蔽智慧仓储摄像头
                        if (item.name.search("智慧仓储") < 0) {

                            var subTree = new Tree({
                                "title": item.name,
                                "list" : []
                            });

                            _this.dealTree(item.children, subTree);

                            rootDom.add(subTree);

                        }

                    }


                });
            }
        },
        "setCameraPosition": {
            value: function (camera) {

                var _this = this;

                if (_this.extendInfos) {


                    $.each(_this.extendInfos, function (channelId, cameraPosition) {

                        if (cameraPosition.channelName === camera.dataObj.channelName) {

                            if (cameraPosition.position) {
                                camera.position = cameraPosition.position;
                            }

                            if (cameraPosition.model) {
                                camera.model = cameraPosition.model;
                            }

                            if (cameraPosition.viewport) {
                                camera.viewport = cameraPosition.viewport;
                            }

                            //根据静态数据读取视频流地址替代后台拉取地址
                            if (cameraPosition.video) {
                                camera.videoURL = cameraPosition.video;
                            }

                            return false;
                        }

                    });
                }
            }
        }
    });

    return _class;

});